#!/usr/bin/env python
import sys
from optparse import OptionParser
import csv
try:
    import json
except ImportError:
    import simplejson as json

parser = OptionParser()
options, args = parser.parse_args()

if args:
    csv_file = open(args[0])
else:
    csv_file = sys.stdin

reader = csv.DictReader(csv_file, delimiter="\t")
rows = [row for row in reader]
for row in rows:
    if "name" in row and "alias" in  row and row["id"] == "1":
        del row["id"]
        del row["owner"]
        row["permissionRoot"] = "false"
    elif "title" in row:
        row["@type"] = "dataset"
        row["files"] = []
        row["versions"] = []
        del row["id"]
        del row["owner"]
        del row["title"]
        del row["author"]
        del row["citationDate"]
        del row["distributor"]
    elif "contentType" in row:
        del row["id"]
        row["permissionRoot"] = "false"
        #print "must be a file..."
        dataset_id = row["dataset"]
        #row["dataset"] = {"id": dataset_id}
    else:
        del row["id"]
        row["permissionRoot"] = "false"
        del row["owner"]
    #if row["id"] == "1":
    print json.dumps(row)
csv_file.close()
# sample dataverse file:
#id	name	alias	owner	contactEmail	description	affiliation
#2	Birds	birds	1	birds@birds.com	A birds dataverse	Birds Inc.
# sample dataset file:
#id	title	author	owner	description	citationDate	distributor
#1	birdstudy1	Dr. Finch	1	About birds	2013-12-11	For the Birds
